#pragma once
#include <vector>
#include <string>
#include <iostream>
#include <jsoncpp/json/json.h>
#include "Color.hpp"

namespace Com_Tools_ns
{
    std::vector<std::string> CutString(const std::string &str, const char &ch)
    {
        std::vector<std::string> ret;
        size_t pos = 0;
        while (pos != std::string::npos && pos < str.size())
        {
            size_t end = str.find(ch, pos);

            ret.push_back(str.substr(pos, end - pos));
            if (end == std::string::npos)
                break;
            pos = end + 1;
        }
        return std::move(ret);
    }

    int Get_FileLine_Count(const std::string &file)
    {
        char cmd[128];
        snprintf(cmd, sizeof(cmd), "cat %s | wc -l", file.c_str());

        FILE *pipe = popen(cmd, "r");
        if (pipe == nullptr)
        {
            return -1;
        }
        int line_count;
        fscanf(pipe, "%d", &line_count);
        return line_count;
    }

    class Bar
    {
        std::string progress_bar(int num, int sz)
        {
            static char bar_pattern = '>';
            std::string ret_bar;

            int bar_len = std::min(num * 100 / sz, 100);
            for (int i = 0; i < bar_len; i++)
                ret_bar.push_back(bar_pattern);

            return std::move(ret_bar);
        }

    public:
        void operator()(int num, int sz)
        {
            static int len = 100;
            static char bar_left = '[';
            static char bar_right = ']';
            std::string ret = progress_bar(num, sz);

            printf(HIDE_CURSOR
                       BOLD_WHITE("\r[") CYAN("%-100s") BOLD_WHITE("]") BOLD_MAGENTA("%3ld%%")
                           SHOW_CURSOR,
                   ret.c_str(), ret.size());
        }
    };

};